package com.mkz.job.feign;

import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.log.annotation.SysLog;
import com.mkz.job.entity.JobInfoVo;
import com.mkz.job.entity.XxlJobGroup;
import com.mkz.job.entity.XxlJobInfo;
import com.mkz.job.feign.JobinfoClient;
import com.mkz.job.service.XxlJobGroupService;
import com.mkz.job.service.XxlJobInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

import java.util.List;

import org.springframework.web.bind.annotation.*;


/**
 * 任务信息
 *
 * @author KuangJianyu
 * @date 2020-04-17 14:33:41
 */
@RestController
@AllArgsConstructor
@Api(value = "xxljobinfo", tags = "任务信息管理")
public class IJobInfoClient implements JobinfoClient {

    private final  XxlJobInfoService xxlJobInfoService;

    private final  XxlJobGroupService xxlJobGroupService;

    /**
              * 通过id查询任务信息
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public ResponseResult<XxlJobInfo> getById(@PathVariable("id" ) Integer id) {
        return ResponseResult.ok(xxlJobInfoService.getById(id));
    }

    /**
              * 新增任务信息
     * @param xxlJobInfo 任务信息
     * @return ResponseResult
     */
    @ApiOperation(value = "新增任务信息", notes = "新增任务信息")
    @SysLog("新增任务信息" )
    @PostMapping
    public ResponseResult<Boolean> save(@RequestBody JobInfoVo jobInfoVo) {
    	List<XxlJobGroup> groupList = xxlJobGroupService.findGroupByAppname(jobInfoVo.getGroupName());
    	if(null==groupList||groupList.size()<1) {
    		return ResponseResult.failed("没有对应的执行器！");
    	}
    	XxlJobInfo xxlJobInfo = new XxlJobInfo(jobInfoVo);
    	xxlJobInfo.setJobGroup(groupList.get(0).getId());
        return ResponseResult.ok(xxlJobInfoService.save(xxlJobInfo));
    }

    /**
              * 修改任务信息
     * @param xxlJobInfo 任务信息
     * @return ResponseResult
     */
    @ApiOperation(value = "修改任务信息", notes = "修改任务信息")
    @SysLog("修改任务信息" )
    @PutMapping
    public ResponseResult<Boolean> updateById(@RequestBody XxlJobInfo xxlJobInfo) {
        return ResponseResult.ok(xxlJobInfoService.updateById(xxlJobInfo));
    }

    /**
              * 通过id删除任务信息
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id删除任务信息", notes = "通过id删除任务信息")
    @SysLog("通过id删除任务信息" )
    @DeleteMapping("/{id}" )
    public ResponseResult<Boolean> removeById(@PathVariable Integer id) {
        return ResponseResult.ok(xxlJobInfoService.removeById(id));
    }
}
